package io_nio;

import java.io.*;

/**
 * ObjectOutputSteam用来处理对象序列化
 * Created by butterfly on 2018/6/28.
 */
public class SerializableDemo {

    public static void main(String[] args) {
        ser();
        unser();
    }

    public static void ser(){
         ObjectOutputStream oos =null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("E:\\pp3\\copyDemo.txt"));
            Person p = new Person("ccccs", 17);
            oos.writeObject(p);//把person对象通过输出流序列化到磁盘
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null){
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void unser(){
        //反序列化
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("E:\\pp3\\copyDemo.txt"));
            Person p  = (Person) ois.readObject();
            System.out.println(p.toString());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (ois != null){
                    ois.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
